﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


//篝火  首先需要将木头放下来
//然后  点燃
//然后  需要不停的添加木头

class GouHuo : SceneObject
{

    public float _huijin;       //灰烬
    public float _totalC;       //总质量中 有效的C 质量      其余的质量 是杂志 不会燃烧 称之为灰烬 当灰烬的比例过高的时候 会影响燃烧效率 需要玩家处理


    public UnityEngine.ParticleSystem fireps;
    public float power;


    float _firing;
    bool _infire = true;
    void UpdatePowerState()
    {
        if(power < 0.1f)
        {
            _firing -= 0.1f - power;
        }
        else
        {
            _firing = 1.0f;
        }
        if(_firing < 0.0f)
        {
            _infire = false;
            ParticleSystem.EmissionModule emission = fireps.emission;
            emission.rateOverTime = 0.0f;
        }
    }


    public override int GetWidth()
    {
        return 1;
    }

    public override int GetHeight()
    {
        return 1;
    }

    public float _mass;

    public override float GetMass()
    {
        return _huijin + _totalC;
    }

    public float Creliang = 25000.0f;
    public float CFireSpped = 3.0f;            //C 燃烧的速度   1mol 每秒     
    float _totaleryanghuatan = 0.0f;

    public int EnergyStartX;
    public int EnergyWidth;
    public int EnergyStartY;
    public int EnergyHeight;
    
    public override void UpdateWrap(float deltaTime)
    {
        if(!_infire)
        {
            return;
        }
        MatterGrid mg = MainGame.Instance.Scene.GetGrid(_x, _y);
        //首先 获得氧气 
        //如果获得足够多的氧气 则产生二氧化碳  
        //如果获得的氧气不够多 则产生一氧化碳
        //如果连一氧化碳都不够 则会熄灭
        //根据获得的氧气 量 产生热量
        // 获得氧气的范围 包括自身 有3 X 3的范围 即 自身的点 左右各1格
        float needo2mol = CFireSpped * deltaTime;       //如果完全燃烧 需要的氧气 mol
        float needCmol = CFireSpped * deltaTime;        //如果完全燃烧 需要的C mol
        float NeedO2 = needo2mol * 0.032f;              //燃烧掉氧气的质量
        float left = mg.RemoveGasMatter("氧气",NeedO2);
        if(left > 0.0f)
        {
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    MatterGrid mg1 = MainGame.Instance.Scene.GetGrid(_x + i, _y + j);
                    if (mg1 != null)
                    {
                        left = mg1.RemoveGasMatter("氧气", left);
                        if (left == 0.0f)
                        {
                            goto L1;
                        }
                    }
                }
            }
        }

    L1: if(left == 0.0f)
        {
            float masseryanghuatan = NeedO2 * 4.4f / 3.2f;
            _totaleryanghuatan += masseryanghuatan;
            //充分燃烧
            mg.AddMatter(new Matter() { currentMatterState = Matter.MatterState.Gas, Mass = masseryanghuatan, matterType = MatterTypeConfig.AllMatters["二氧化碳"], Temperature = _temperature });
            AddEnegy(needo2mol * Creliang);   //每mol 的碳提供 1000kj的能量
        }
        else if(left < NeedO2 / 2.0f)        //1mol 的C 燃烧消耗0.5mol的 一氧化碳 产生 1mol 的 一氧化碳
        {
            // X mol 的碳 燃烧产生二氧化碳
            // Y mol 的碳 燃烧产生一氧化碳
            // X + 0.5Y = NeedO2 - LEFT;
            // X + Y = needCmol
            // 解方程  
            float molyiyanghuatanC = 2.0f * needCmol - 2 * (NeedO2 - left) / 0.032f;
            float moleryanghuatanC = needCmol - molyiyanghuatanC;
            mg.AddMatter(new Matter() { currentMatterState = Matter.MatterState.Gas, Mass = molyiyanghuatanC * 0.028f, matterType = MatterTypeConfig.AllMatters["一氧化碳"], Temperature = _temperature });
            mg.AddMatter(new Matter() { currentMatterState = Matter.MatterState.Gas, Mass = moleryanghuatanC * 0.044f, matterType = MatterTypeConfig.AllMatters["二氧化碳"], Temperature = _temperature });
            //同时产生热量  不充分燃烧产生的热量 会减少    先凑合算一半吧.
            AddEnegy(Creliang * molyiyanghuatanC + Creliang * 0.5f * moleryanghuatanC);
            //不充分燃烧
        }
        else if(left > NeedO2 /2.0f)
        {
            //充分燃烧都不够 产生的热量开始减少
            //全部产生一氧化碳
            float used02 = NeedO2 - left;
            float o2mol = used02 / 0.032f;
            float molyiyanghuatan = o2mol * 2;
            mg.AddMatter(new Matter() { currentMatterState = Matter.MatterState.Gas, Mass = molyiyanghuatan * 0.028f, matterType = MatterTypeConfig.AllMatters["一氧化碳"], Temperature = _temperature });
            AddEnegy(Creliang * 0.5f * molyiyanghuatan);
        }
        power = (1.0f - left / NeedO2);

        UpdateFireView();
        UpdatePowerState();
    }


    void UpdateFireView()
    {
        ParticleSystem.EmissionModule emission = fireps.emission;
        emission.rateOverTime = 100.0f * power;
    }

    void AddEnegy(float energy)
    {
        int count = 0;
        for (int i = EnergyStartX; i < EnergyStartX + EnergyWidth; i++)
        {
            for (int j = EnergyStartY; j < EnergyStartX + EnergyHeight; j++)
            {
                MatterGrid mg1 = MainGame.Instance.Scene.GetGrid(_x + i, _y + j);
                if (mg1 != null)
                {
                    count++;
                }
            }
        }
        if (count == 0)
        {
            return;
        }
        float addper = energy / count;
        for (int i = EnergyStartX; i <=EnergyStartX + EnergyWidth; i++)
        {
            for (int j = EnergyStartY; j < EnergyStartX + EnergyHeight; j++)
            {
                MatterGrid mg1 = MainGame.Instance.Scene.GetGrid(_x + i, _y + j);
                if (mg1 != null)
                {
                    mg1.EnergyChange(addper);
                }
            }
        }
    }

    public override void Init()
    {

    }
}
